package com.haiya.identityaccess.domain;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * 领域注册器: 使用Spring容器来获取bean
 * Ax ax = (Ax) DomainRegistry.getBean(Ax.class);
 *
 * 对于new出来的对象，是不经过Spring容器管理的，如果需要使用到容器中的Bean, 可采用这种方式来获取。
 * @author qiaoguoqiang
 */
@Component
public class DomainRegistry  implements ApplicationContextAware {
    private static ApplicationContext applicationContext;

    @Override
    public final void setApplicationContext(ApplicationContext ctx)
            throws BeansException {
        DomainRegistry.applicationContext = ctx;
    }

    public final static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public static Object getBean(Class clazz) throws BeansException {
        return applicationContext.getBean(clazz);
    }

    public static Object getBean(String name) throws BeansException {
        return applicationContext.getBean(name);
    }
}
